package com.xywy.wjk.widget;

import java.util.Timer;
import java.util.TimerTask;

import com.xywy.R;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Path;
import android.graphics.Path.Direction;
import android.graphics.RectF;
import android.graphics.Region;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
/**
 * 体温计 图
 * @author wwl
 *
 */
public class TemperatureView extends View {
	private Context context;
    private Paint paint_circle;
    private Paint paint_weight ;
    private Paint paint_gray;
    private Paint paint_text;
    
    private int color_light_black = 0xff333333;
    private int  dark_color = 0xfffc7148; //深的颜色
    private int color_wheight = 0xffffffff;
    private int color_gray = 0xffe5e5e5;
    private int sum_width;
    private int sum_height;
    private float inner_height ; //内圆直径
    private float inner_top ;  //内圆顶部
    private float inner_bottom ;  //内圆底部
    private float inner_left ;  //内圆左边
    private float inner_right ;  //内圆右边
    private float r_in; //内圆半径
    private float center_x;
    private float center_y;
    
    private float r_out;
    
    private float raidus_diff = 50;
    
    private float padding = 30;
    
    private int start_over = 0;
    private float start_over_low = 0;
    
    private float temperature_value = 0;
    private Bitmap bitmap_tip;
    
    float degree_tip = (float) (Math.PI/2);
    
    private Bitmap bitmap_dark;
    private Bitmap bitmap_alpha;
    private float bmp_width;
    
    public float getTemperature_value() {
		return temperature_value;
	}

	public void setTemperature_value(float temperature_value) {
		this.temperature_value = temperature_value;
		invalidate();
	}
	Handler handler = new Handler(){
    	public void handleMessage(android.os.Message msg) {
    		invalidate();
    	};
    };
    public TemperatureView(Context context) {
		super(context);
		this.context = context;
		init();
	}

	public TemperatureView(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context = context;
		init();
	}

	public TemperatureView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		this.context = context;
		init();
	}
	
	private void init(){
		paint_circle = new Paint();
		paint_circle.setColor(dark_color);
		paint_circle.setStyle(Style.STROKE);
		paint_circle.setStrokeWidth(5);
		
		paint_weight = new Paint();
		paint_weight.setColor(color_wheight);
		
		paint_gray = new Paint();
		paint_gray.setColor(color_gray);
		paint_gray.setStyle(Style.STROKE);
		paint_gray.setStrokeWidth(5);
		bitmap_tip = BitmapFactory.decodeResource(context.getResources(), R.drawable.tempeture_tip);
		
		Timer timer = new Timer();
		timer.schedule(new TimerTask() {
			
			@Override
			public void run() {
				if((inner_right-bmp_width+ current_position+diff_width_bitmap) >= inner_left){
					timer_change_flag = true;
				}else if ((inner_right+ current_position)<inner_right) {
					timer_change_flag = false;
				}
				if(timer_change_flag){
					current_position--;
				}else {
					current_position++;
				}
				handler.sendEmptyMessage(0);
			}
		}, 100, 5);
		
		bitmap_dark = BitmapFactory.decodeResource(context.getResources(), R.drawable.t_dark);
		bitmap_alpha = BitmapFactory.decodeResource(context.getResources(),R.drawable.t_alpha);
		bmp_width = bitmap_dark.getWidth();
		
		paint_text = new Paint();
		paint_text.setTextSize(50);
		paint_text.setColor(color_light_black);
	}
	
	@Override
	protected void onDraw(Canvas canvas) {
		canvas.drawColor(Color.WHITE);
		
		sum_width = getMeasuredWidth();
		sum_height = getMeasuredHeight();
		float circle_point[] = {
				sum_width/2,	
				sum_height/2	
		};
		center_x = circle_point[0];
		center_y = circle_point[1];
		r_out = sum_height/2-padding;
		r_in = r_out - raidus_diff;
		inner_height = r_in*2;
		inner_top = sum_height/2-r_out+raidus_diff;
		inner_bottom = inner_top+inner_height;
		inner_left = sum_width/2-r_out+raidus_diff;
		inner_right = inner_left+inner_height;
		
		Path p = new Path();
		p.moveTo(circle_point[0], inner_top);
		p.addCircle(circle_point[0], circle_point[1], r_in, Direction.CCW);
		canvas.clipPath(p, Region.Op.INTERSECT);
		
		degree = temperature_value/80;
		drawDark(canvas);
		drawAlpha(canvas);
		canvas.clipRect(0, 0, sum_width, sum_height, Region.Op.UNION);  
		canvas.drawCircle(circle_point[0], circle_point[1], r_out, paint_gray);
	    //尝试画一个弧
		RectF oval = new RectF();
		oval.left = sum_width/2-r_out;
		oval.top = sum_height/2-r_out;
		oval.right = r_out*2+oval.left;
		oval.bottom = oval.top+2*r_out;
		float text_width = paint_text.measureText(temperature_value+"℃");
		canvas.drawText(temperature_value+"℃", center_x-text_width/2, inner_top+90, paint_text);
		
	}
    float degree = 0;
    
    private float diff_width_bitmap = 50;
    private float current_position = 0;
    private void drawDark(Canvas canvas){
		float percent = (inner_height*degree);
	    float y_temp = sum_height - percent;
	    canvas.drawBitmap(bitmap_dark, inner_right-bmp_width+current_position, y_temp, paint_circle);
    }
    boolean timer_change_flag = false;
    
    private void drawAlpha(Canvas canvas){
    	float percent = (inner_height*degree);
 	    float y_temp = sum_height - percent;
 	    canvas.drawBitmap(bitmap_alpha,inner_right-bmp_width+ current_position+diff_width_bitmap, y_temp, paint_circle);
    }
//	private void drawSai(Canvas canvas){
//	    //画一个完整的sin函数
//		//width相当于 2*pi
//		float percent = (inner_height*degree)/360;
////		float h_temp = percent*inner_height; //这个线的高度
//		float y_temp = center_y + percent;
//		int sin_height = sum_height/15;
//		float unit_w = 2;
//		float avg_degree = (float) (4*Math.PI*(unit_w/sum_width));
//		Path p = new Path();
//		p.moveTo(0, sum_height);
//		p.lineTo(0, y_temp);
//		for(int i=0;unit_w*i<=sum_width;i++){
//			float y =  (float) (y_temp - Math.sin(avg_degree*(start_over-i))*sin_height);
//			p.lineTo(i*unit_w,y);
//		}
//		
//		p.lineTo(sum_width, sum_height);
//		p.close();
//		Paint paint_path = new Paint();
//		paint_path.setStyle(Style.FILL);
//		paint_path.setColor(dark_color);
//		canvas.drawPath(p, paint_path);
//		
//	}
//	private void drawSai1(Canvas canvas){
//		//画一个完整的sin函数
//		//width相当于 2*pi
//		float percent = (inner_height*degree)/360;
////		float h_temp = percent*inner_height; //这个线的高度
//		float y_temp = center_y + percent;
//		int sin_height = 30;
//		float unit_w = 2;
//		float avg_degree = (float) (4*Math.PI*(unit_w/sum_width));
//		Path p = new Path();
//		p.moveTo(0, sum_height);
//		p.lineTo(0, y_temp);
//		for(int i=0;unit_w*i<=sum_width;i++){
//			float y =  (float) (y_temp - Math.sin(avg_degree*(start_over_low-i))*sin_height);
//			p.lineTo(i*unit_w,y);
//		}
//		
//		p.lineTo(sum_width, sum_height);
//		p.close();
//		Paint paint_path = new Paint();
//		paint_path.setStyle(Style.FILL);
//		paint_path.setColor(0xffffac60);
//		canvas.drawPath(p, paint_path);
//	}
	
}
